package com.ak.demo.controller;

import com.ak.demo.config.UrlComponent;
import com.ak.demo.service.impl.LoginServiceImpl;
import com.ak.demo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 处理csdn 的模块接口
 */
@Controller
@Slf4j
public class CsdnController {


    /**
     * 添加链接的地方
     * @return
     */
    @RequestMapping("toadd")
    public String toAdd(){
        return "toadd";
    }

    @Autowired
    LoginServiceImpl service;

    /**
     * 展示全部链接
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        try {
            if(UrlComponent.list.size()>0){
                model.addAttribute("size",UrlComponent.list.size());
                model.addAttribute("list",UrlComponent.list);
                model.addAttribute("count",UrlComponent.count);
                model.addAttribute("start",UrlComponent.start);
                if(UrlComponent.start){
                    model.addAttribute("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(UrlComponent.date));
                }
            }else {
                log.info("对不起用户数量为 0");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "list";
    }

    /**
     * 添加用户信息，然后开始 解析
     * @param username
     * @return
     */
    @PostMapping("parse")
    @ResponseBody
    public String parse2( @RequestParam String username){
        List<String> tmp= ResponseUtils.parse(username);
        if(tmp.size()>0){
            for(String s:tmp){
                if(!UrlComponent.list.contains(s)){
                    UrlComponent.list.add(s);
                }
            }
            return "添加成功";
        }else {
            log.info("parse 添加失败");
            return "添加失败";
        }
    }

    @RequestMapping("/testEmail")
    @ResponseBody
    public Boolean test(String to,String body){
        return service.email(body,to);
    }

    @RequestMapping("/email")
    public String email(){
        return "email";
    }

}
